package it.koutifaris.travianmap.view.mainview;

import it.koutifaris.travianmap.R;
import it.koutifaris.travianmap.communication.mapdownloader.DownloadProgressHandler;
import it.koutifaris.travianmap.context.HasApplicationContex;

import java.text.DateFormat;
import java.util.Date;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class ServerItemViewImpl extends HasApplicationContex implements ServerItemView {

	private static final String TAG = ServerItemViewImpl.class.getName();

	private View androidView;

	public ServerItemViewImpl(View androidView) {
		this.androidView = androidView;
		getImageView().setVisibility(ImageView.INVISIBLE);
	}

	@Override
	public ServerItemView setIconClickHandler(OnClickListener handler) {
		ImageView imageView = getImageView();
		imageView.setVisibility(ImageView.VISIBLE);
		imageView.setOnClickListener(handler);

		return this;
	}

	private ImageView getImageView() {
		return (ImageView) androidView.findViewById(R.id.map_server_downloadImg);
	}

	@Override
	public ServerItemView setLabel(String label) {
		TextView textView = getLabel();
		textView.setText(label);

		return this;
	}

	private TextView getLabel() {
		TextView textView = (TextView) androidView.findViewById(R.id.map_server_label);
		return textView;
	}

	@Override
	public ServerItemView setlastUpdate(Date lastUpdate) {
		if (lastUpdate == null) {
			getLastUpdate().setText(R.string.maplist_lastupdate_na);
		} else {
			DateFormat dateFormat = getTravianMapContext().getLocaleSettings().getDefaultDateFormat();
			getLastUpdate().setText(dateFormat.format(lastUpdate));
		}

		return this;
	}

	private TextView getLastUpdate() {
		TextView textView = (TextView) androidView.findViewById(R.id.map_server_lastupdate);
		return textView;
	}

	@Override
	public DownloadProgressHandler getDownloadProgressHandler(OnDownloadFinishedHandler finishHandler) {
		return new DownloadProgressHandlerImpl(finishHandler);
	}

	private ProgressBar getProgressBar() {
		return (ProgressBar) androidView.findViewById(R.id.map_server_progressBar);
	}

	private class DownloadProgressHandlerImpl implements DownloadProgressHandler {

		private long contentLength;
		private long byteCount;
		private final OnDownloadFinishedHandler finishhandler;
		private static final int MAX = 100;

		public DownloadProgressHandlerImpl(OnDownloadFinishedHandler finishhandler) {
			this.finishhandler = finishhandler;
		}

		@Override
		public void onPreInit() {
			initProgressBar();
		}

		@Override
		public void init(long contentLength) {
			this.contentLength = contentLength;
		}

		@Override
		public void onProgress(long byteCount) {
			this.byteCount = byteCount;
			int done = (int) ((byteCount * MAX) / contentLength);
			getProgressBar().setProgress(done);
		}

		@Override
		public void updateUI(View view) {
			ServerItemViewImpl.this.androidView = view;
			initProgressBar();

			if (contentLength > 0) {
				onProgress(byteCount);
			}
		}

		private void initProgressBar() {
			getProgressBar().setVisibility(View.VISIBLE);
			getProgressBar().setMax(MAX);
		}

		@Override
		public void onFinish(long byteCount) {
			Toast.makeText(androidView.getContext(), R.string.maplist_downloadComplete, Toast.LENGTH_SHORT).show();
			getProgressBar().setVisibility(View.INVISIBLE);
			finishhandler.onDownloadFinished();
		}

		@Override
		public void onError(Exception e) {
			Toast.makeText(androidView.getContext(), R.string.maplist_download_error, Toast.LENGTH_SHORT).show();
			Log.e(TAG, "Error on downloading: " + e.getMessage());
		}

	}

}
